package org.gitman.agentplatform.agent.api.utils;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.gitman.agentplatform.agent.api.AgentInfo;
import org.gitman.agentplatform.agent.api.AgentPlan;

public class AgentTools {
	
	/**
	 * This method return collections of vertices that should be avoided at given time.
	 * If any agent have multiple plans (e.g. 2 plans with 0.5 probability) returned map will
	 * contain all listed vertices 
	 * @param agentInfo 
	 * @param agents
	 * @return
	 */
	public static Map<Integer, Set<Integer>> getVertices2avoid(Collection<AgentInfo> agents, boolean moveNow){
		Map<Integer, Set<Integer>> map = new HashMap<Integer, Set<Integer>>();
		int shift = 1;
		if(moveNow){
			shift++;
			map.put(1, new HashSet<Integer>());
			for(AgentInfo info : agents){
				map.get(1).add(info.getCurrentVertexID());
			}
		}		
		for(AgentInfo info : agents){
			AgentPlan agentPlan = info.getAgentPlan();
			if(agentPlan != null ){
				Map<Integer[], Double> plans = info.getAgentPlan().getPlans();
				for(Integer[] tab: plans.keySet()){
					for(int i =0;i<tab.length;i++){
						Set<Integer> set = map.get(i+shift);
						if(set == null){
							set = new HashSet<Integer>();
							map.put(i+shift, set);
						}
						set.add(tab[i]);
	 				}
				}
			}
		}	
		return map;
	}
}
